package com.oddfar.ztwz.business.controller.web;

import com.oddfar.ztwz.business.domain.entity.Friend;
import com.oddfar.ztwz.business.service.FriendService;
import com.oddfar.ztwz.common.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.oddfar.ztwz.common.utils.SecurityUtils.getUserId;

@RestController
@RequestMapping("/friend")
@Slf4j
public class FriendController {
    @Autowired
    private FriendService friendService;
    @PostMapping("/addFriend")

    public R addFriend(@RequestBody Friend friend) {
        Long userId = getUserId();
        Long friendUserId = friend.getFriendUserId();

        // 检查是否已经是好友关系
        if (friendService.existsFriendship(userId, friendUserId)) {
            return R.error("已经是好友关系或已经发送申请，不能重复添加");
        }

        friend.setUserId(userId);

        boolean save = friendService.save(friend);
        if (!save) {
            return R.error("添加失败");
        }
        return R.ok();
    }


    @GetMapping("/getFriendListByMyId/{status}")
    public R getFriendListByMyId(@PathVariable String status) {
        Long userId = getUserId();
        return friendService.getFriendListByMyId(userId,status);
    }
    @DeleteMapping("/removeFriendById/{friendId}")
    public R removeFriendById(@PathVariable String friendId) {
        Long userId = getUserId();
        boolean remove = friendService.removeFriend(userId, Long.valueOf(friendId));
        if (!remove) {
            return R.error("删除失败");
        }
        return R.ok();
    }
    @PutMapping("/approveOrrejectFriend/{friendId}/{status}")
    public R approveOrrejectFriend(@PathVariable Long friendId,@PathVariable Integer status) {
        Long userId = getUserId();
        Friend friend = new Friend();
        friend.setFriendUserId(friendId);
        friend.setUserId(userId);
        friend.setStatus(status);
        boolean update = friendService.approveOrrejectFriend(friend);
        if (!update) {
            return R.error("操作失败");
        }
        return R.ok();
    }




}
